package com.koicarp.agent.example.chatmemery;


import com.koicarp.agent.example.ChatModelInit;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;

/**
 * @Author: liuxia
 * @CreateTime: 2025/9/13 下午10:06
 * @Description: 消息窗口聊天缓存测试
 */
public class MessageWindowChatMemoryTest {

    public static void main(String[] args) {
        ChatModel model = ChatModelInit.initOllama();

        MessageWindowChatMemory memory = MessageWindowChatMemory.builder()
                .id("1234")                 // id，通常用于区分多个用户或多个对话
                .maxMessages(2)             // 最大存储消息数量
                .chatMemoryStore(new InMemoryChatMemoryStore())   //内存缓存存储类
                .build();

        UserMessage oneMsg = UserMessage.from("你好，你现在是我的朋友koi");
        memory.add(oneMsg);

        AiMessage oneAiMsg = model.chat(memory.messages()).aiMessage();
        System.out.println("ai回答1： " + oneAiMsg.text());

        UserMessage twoMsg = UserMessage.from("你是谁?");
        memory.add(twoMsg);

        AiMessage twoAiMsg = model.chat(memory.messages()).aiMessage();
        System.out.println("ai回答2： " + twoAiMsg.text());

        UserMessage threeMsg = UserMessage.from("你是叫koi吗");
        memory.add(threeMsg);

        for (int i = 0; i < memory.messages().size(); i++) {        // 只会打印最新的两条信息，删除了第一条
            System.out.println("第" + i + "条信息: "+ memory.messages().get(i).toString());
        }

        AiMessage threeAiMsg = model.chat(memory.messages()).aiMessage();
        System.out.println("ai回答3： " + threeAiMsg.text());
    }
}
